package zhou.chs.p3_1;

import android.content.Context;
import android.os.Environment;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

/**
 * Created by Administrator on 2018/9/17 0017.
 */

public class FileService{
    // 内置存储文件写操作
    public static boolean saveFile(Context context, String filename, String str){
        File file = new File(context.getFilesDir(), filename);
        try {
            FileWriter fw = new FileWriter(file);
            fw.write(str);
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    // 扩展存储文件写操作
    public static boolean saveFileEx(Context context,String filename, String str){
        File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), filename);
        try {
            FileWriter fw = new FileWriter(file);
            fw.write(str);
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    // 公共存储文件写操作
    public static boolean saveFileSD(Context context,String filename, String str){
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), filename);
        //File file = new File("/storage/emulated/0/", filename);  //指定位置存储
        try {
            FileWriter fw = new FileWriter(file);
            fw.write(str);
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    // 内置存储文件读操作
    public static String readFile(Context context, String filename){
        File file = new File(context.getFilesDir(), filename);
        if (!file.exists()) {   //判断文件是否存在
            return null;       //如果文件不存在则返回null
        }
        String result = null;
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            result = reader.readLine();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result ;
    }
    // 扩展存储文件读操作
    public static String readFileEx(Context context, String filename){
        File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), filename);
        if (!file.exists()) {
            return null;
        }
        String result = null;
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            result = reader.readLine();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result ;
    }
    // 公共存储文件读操作
    public static String readFileSD(Context context, String filename){
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), filename);
        if (!file.exists()) {
            return null;
        }
        String result = null;
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            result = reader.readLine();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result ;
    }
    // 删除公共存储文件
    public static boolean deleteFileSD(Context context, String filename){
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), filename);
        try {
            return file.delete();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
